package com.zeedev.flyingsaucer.adapter;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.net.Uri.Builder;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.UnderlineSpan;
import android.view.Gravity;
import android.view.View;
import android.widget.*;
import com.zeedev.flyingsaucer.ufoclub.R;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv.Beers;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv.Members;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv.Members.MemberBeer;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv.Stores;
import mobi.zeedev.flyingsaucer.common.DataModule.MetaBeer;
import mobi.zeedev.flyingsaucer.common.DataModule.Util;

public class BeerTreeAdapter extends ResourceCursorTreeAdapter {

	private static final String TAG = "BeerTreeAdapter";
	private Boolean isStore = false;

	public BeerTreeAdapter(Context context, Cursor cursor, String columnName, Boolean isStore) {
		super(context, cursor, R.layout.list_text, R.layout.beer_indicator_tree_child);
		mContext = context;
		this.columnName = columnName;
//		this.uriSpace = uriSpace;
		this.isStore = isStore;
	}

	private Context mContext;
	private String columnName;
//	private BaseColumns uriSpace;

	@Override
	protected void bindChildView(View view, Context context, Cursor cursor,	boolean isLastChild) {

        LinearLayout parent =  (LinearLayout) view.findViewById(R.id.beer_indicator_tree_container);
        TextView beerNameView = (TextView) view.findViewById(R.id.beer_indicator_tree_text);
        ImageView beerContainerView = (ImageView) view.findViewById(R.id.beer_indicator_tree_image);

        Boolean isCan = cursor.getInt(cursor.getColumnIndex(MetaBeer.CAN)) == 1 ? true : false;
        Boolean isDraught = cursor.getInt(cursor.getColumnIndex(MetaBeer.DRAUGHT)) == 1 ? true : false;
        Boolean isBottled = cursor.getInt(cursor.getColumnIndex(MetaBeer.BOTTLE)) == 1 ? true : false;
        
        if (isCan) {
        	beerContainerView.setImageDrawable( context.getResources().getDrawable(R.drawable.can) );
        } else
        if (isDraught) {
        	beerContainerView.setImageDrawable( context.getResources().getDrawable(R.drawable.draught) );
        } else
        if (isBottled) {
        	beerContainerView.setImageDrawable( context.getResources().getDrawable(R.drawable.bottle) );
        }

        beerNameView.setText(cursor.getString(cursor.getColumnIndex(Beers.NAME)));

        beerNameView.setTextAppearance(context,R.style.DateStylesChild);

        parent.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        //public void setPadding (int left, int top, int right, int bottom)
        if (isLastChild) {
        	parent.setPadding(36, 3, 0, 5);
        } else {
        	parent.setPadding(36, 3, 0, 0);
        }
	}

	@Override
	protected void bindGroupView(View view, Context context, Cursor cursor,	boolean isExpanded) {

        //This keeps the button from being bound to, which I think represents the group expander.
        if (view.getClass() == Button.class) {
            return;
        }
		
        TextView textView = (TextView) view.findViewById(R.id.text);
//        Button textView = (Button) view.findViewById(R.id.text);

        textView.setTextAppearance(context,R.style.DateStylesGroup);

//        String filterProperty = "";
        String textDisplay = "";
//    	filterProperty = groupBy;
//        String column = Util.getFieldsValue(mContext, "groupBy", String.class);
        if (columnName == null) {
//        	column = Beers.BEER_ID;
        	return;
        }
//    	textDisplay = cursor.getString(cursor.getColumnIndex(filterProperty));
        textDisplay = cursor.getString(cursor.getColumnIndex(columnName));
        SpannableString content = null;
        if (!TextUtils.isEmpty(textDisplay)) {
        	 content = new SpannableString(textDisplay);//cursor.getColumnIndex(mMode == DisplayMode.STYLE ? MetaBeer.STYLE_NAME : MetaBeer.SIMPLE_DATE))
        } else {
       	 	content = new SpannableString("");
        }
        content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
        textView.setText(content);
        
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);

        textView.setPadding(18, 0, 0, 0);
	}

	@Override
	protected Cursor getChildrenCursor(Cursor groupCursor) {

		String groupId = groupCursor.getString(groupCursor.getColumnIndex(columnName));
        String identKeyId = null;

        if ( this.isStore ) {
        	identKeyId = groupCursor.getString(groupCursor.getColumnIndex(Stores.StoreBeer.STORE_DB_ID));
        } else {
        	identKeyId = groupCursor.getString(groupCursor.getColumnIndex(Members.UFO_MEMBER_ID));
        }
        
        Cursor cursor = null;
        
        Activity act = (Activity) mContext;
        
        if (this.isStore) {
        	Uri turi = Stores.StoreBeer.FILTER_CONTENT_URI;
        	Builder b = turi.buildUpon();
			b.appendQueryParameter(FlyingSaucerConv.ACTIVE_BY, "true");
			b.appendQueryParameter(FlyingSaucerConv.BY_STORE_DB_ID, identKeyId);
			b.appendQueryParameter(FlyingSaucerConv.SORT_BY, Beers.NAME + " ASC");
			
        	String[] tpro = Util.asArNonReduce(Stores.StoreBeer.STANDARD_COLUMNS, columnName, Stores.StoreBeer.CREATION_DATE, Stores.StoreBeer.UPDATED_DATE);
        	cursor = act.managedQuery(
        			b.build(), 
        			tpro,
        			columnName + "=?", 
        			Util.asAr(groupId), 
        			null);
        } 
        else {
        	Uri turi = Members.MemberBeer.FILTER_CONTENT_URI;
        	Builder b = turi.buildUpon();
        	
        	if (groupCursor.getColumnIndex(MemberBeer.SIMPLE_CREATION_DATE) < 0) {
        		b.appendQueryParameter(FlyingSaucerConv.TASTED_BY, "true");
        	} else {
        		b.appendQueryParameter(FlyingSaucerConv.TASTED_BY, "false");
        	}
			
        	if (groupCursor.getColumnIndex(MemberBeer.SIMPLE_TASTED_DATE) < 0) {
        		
        	} else {
        		
        	}
        	b.appendQueryParameter(FlyingSaucerConv.SORT_BY, Beers.NAME + " ASC");
        	
        	
			
			
        	String[] tpro = Util.asArNonReduce(Members.MemberBeer.STANDARD_COLUMNS, columnName, MemberBeer.IS_TASTED, MemberBeer.ACTIVE, MemberBeer.CREATION_DATE, MemberBeer.TASTED_DATE);
        	cursor = act.managedQuery(
        			b.build(), 
        			tpro, //projection, 
        			columnName + "=? AND " + Members.UFO_MEMBER_ID + "=?",//selection, 
        			Util.asAr(groupId, identKeyId),
        			null);
        }
        
        
		
        return cursor;
	}

	@Override
	public long getGroupId(int groupPosition) {

//		CursorTreeAdapter c = Util.getFindField(this, "mGroupCursorHelper", CursorTreeAdapter.class);
//		Cursor c2 = Util.getFindField(c, "mCursor", Cursor.class);
//		DatabaseUtils.dumpCursor(this.getCursor());
		return groupPosition;
	}

}
